Grokking Dynamic Programming Patterns for Coding Interviews
Rate this course
Ask Author
Contribute
Back to course home

0% completed

Vote For New Content
Introduction
What is Dynamic Programming?
Pattern 1: 0/1 Knapsack
Introduction
0/1 Knapsack
Solution: 0/1 Knapsack
Equal Subset Sum Partition
Solution: Equal Subset Sum Partition
Subset Sum
Solution: Subset Sum
Minimum Subset Sum Difference
Solution: Minimum Subset Sum Difference
Count of Subset Sum
Solution: Count of Subset Sum
Target Sum
Solution: Target Sum
Pattern 2: Unbounded Knapsack
Unbounded Knapsack
Solution: Unbounded Knapsack
Rod Cutting
Solution: Rod Cutting
Coin Change
Solution: Coin Change
Minimum Coin Change
Solution: Minimum Coin Change
Maximum Ribbon Cut
Solution: Maximum Ribbon Cut
Pattern 3: Fibonacci Numbers
Fibonacci numbers
Solution: Fibonacci numbers
Staircase
Solution: Staircase
Number factors
Solution: Number factors
Minimum jumps to reach the end
Solution: Minimum jumps to reach the end
Minimum jumps with fee
Solution: Minimum jumps with fee
House thief
Solution: House thief
Longest Palindromic Subsequence
Solution: Longest Palindromic Subsequence
Longest Palindromic Substring
Solution: Longest Palindromic Substring
Count of Palindromic Substrings
Solution: Count of Palindromic Substrings
Minimum Deletions in a String to make it a Palindrome
Solution: Minimum Deletions in a String to make it a Palindrome
Palindromic Partitioning
Solution: Palindromic Partitioning
Longest Common Substring
Solution: Longest Common Substring
Longest Common Subsequence
Solution: Longest Common Subsequence
Minimum Deletions & Insertions to Transform a String into another
Solution: Minimum Deletions & Insertions to Transform a String into another
Longest Increasing Subsequence
Solution: Longest Increasing Subsequence
Maximum Sum Increasing Subsequence
Solution: Maximum Sum Increasing Subsequence
Shortest Common Super-sequence
Solution: Shortest Common Super-sequence
Minimum Deletions to Make a Sequence Sorted
Solution: Minimum Deletions to Make a Sequence Sorted
Longest Repeating Subsequence
Solution: Longest Repeating Subsequence
Subsequence Pattern Matching
Solution: Subsequence Pattern Matching
Longest Bitonic Subsequence
Solution: Longest Bitonic Subsequence
Longest Alternating Subsequence
Solution: Longest Alternating Subsequence
Edit Distance
Solution: Edit Distance
Strings Interleaving
Solution: Strings Interleaving
Contact Us
Solution: Number factors

Problem Statement

Given a number 'n', implement a method to count how many possible ways there are to express 'n' as the sum of 1, 3, or 4.

Example 1:

n : 4
Number of ways = 4
Explanation: Following are the four ways we can express 'n' : {1,1,1,1}, {1,3}, {3,1}, {4} 

Example 2:

n : 5
Number of ways = 6
Explanation: Following are the six ways we can express 'n' : {1,1,1,1,1}, {1,1,3}, {1,3,1}, {3,1,1}, 
{1,4}, {4,1}

Let's first start with a recursive brute-force solution.

Basic Solution

For every number 'i', we have three option: subtract either 1, 3, or 4 from 'i' and recursively process the remaining number. So our algorithm will look like:

Java
Saved to Cloud

. . . .

The time complexity of the above algorithm is exponential . The space complexity is which is used to store the recursion stack.

Let's visually draw the recursion for CountWays(5) to see the overlapping subproblems:

Recursion tree for calculating Fibonacci numbers
Recursion tree for calculating Fibonacci numbers

We can clearly see the overlapping subproblem pattern: CountWays(3), CountWays(2) and CountWays(1) have been called twice. We can optimize this using memoization to store the results for subproblems.

Top-down Dynamic Programming with Memoization

We can use an array to store the already solved subproblems. Here is the code:

Java
Saved to Cloud

. . . .

Bottom-up Dynamic Programming

Let's try to populate our dp[] array from the above solution, working in a bottom-up fashion. As we saw in the above code, every CountWaysRecursive(n) is the sum of the three counts. We can use this fact to populate our array.

Code

Here is the code for our bottom-up dynamic programming approach:

Java

. . . .

The above solution has a time and space complexity of and respectively.

Fibonacci number pattern

We can clearly see that this problem follows the Fibonacci number pattern. However, every number in a Fibonacci series is the sum of the previous two numbers, whereas in this problem every count is a sum of previous three numbers: previous-1, previous-3, and previous-4. Here is the recursive formula for this problem:

CountWays(n) = CountWays(n-1) + CountWays(n-3) + CountWays(n-4), for n >= 4
Number factors
Minimum jumps to reach the end
Mark as Completed

On this page

Problem Statement

Basic Solution

Top-down Dynamic Programming with Memoization

Bottom-up Dynamic Programming

Code

Fibonacci number pattern